<template>
  <div class="loginform_wrap">
    <el-form :model="form" :rules="rules" ref="loginForm">
      <el-form-item prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <div class="forget_wrap">
        <nuxt-link to="#">忘记密码？</nuxt-link>
      </div>
      <el-form-item>
        <el-button class="login_btn" type="primary" @click="submitForm"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: "admin",
        password: "admin",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm() {
      // 1.校验输入是否合法 利用element内置整个表单的验证功能
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          // 2.通过调用vuex中的action来完成 发送请求
          const res = await this.$store.dispatch("user/login", this.form);
          //  console.log('登录了');
          // 3提示成功
          this.$message({
            message: "登录成功",
            type: "success",
            duration: 1500,
          });
          setTimeout(()=>{
            this.$router.replace('/')
          },1500)
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.loginform_wrap {
  padding: 25px;
  .forget_wrap {
    text-align: right;
    padding-bottom: 10px;
    a {
      color: #66b1ff;
      font-size: 12px;
    }
  }
  .login_btn {
    width: 100%;
  }
}
</style>